package com.miao.util.mpeg;

import org.apache.commons.lang.StringUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by miao on 2015/4/10.
 */
public class ConvertVideo {
    static final String[] ffmpegCoder = {"asx", "asf", "mpg", "wmv", "3gp", "mp4", "mov", "avi", "flv"};
    static final String[] mencoder = {"wmv9", "rm", "rmvb"};
    static String CMD = "D:\\studio\\apps\\web\\mpeg\\";

    private static int getType(String name) {
        for (String code : ffmpegCoder) {
            if (StringUtils.endsWithIgnoreCase(name, "." + code)) {
                return 1;
            }
        }
        for (String code : mencoder) {
            if (StringUtils.endsWithIgnoreCase(name, "." + code)) {
                return 2;
            }
        }
        return 0;
    }

    private static void toAvi(String src, String dic) throws IOException {
        List commend = new ArrayList();
        commend.add(CMD + "mencoder");
        commend.add(src);
        commend.add("-oac");
        commend.add("lavc");
        commend.add("-lavcopts");
        commend.add("acodec=mp3:abitrate=64");
        commend.add("-ovc");
        commend.add("xvid");
        commend.add("-xvidencopts");
        commend.add("bitrate=600");
        commend.add("-of");
        commend.add("avi");
        commend.add("-o");
        commend.add(dic);
        ProcessBuilder builder = new ProcessBuilder(new String[0]);
        builder.command(commend);
        System.out.println(commend.toString());
        builder.start();
    }

    private static void toFlv(String src, String dic) throws IOException {
        List commend = new ArrayList();
        commend.add(CMD + "ffmpeg");
        commend.add("-i");
        commend.add(src);
        commend.add("-ab");
        commend.add("64");
        commend.add("-acodec");
        commend.add("mp3");
        commend.add("-ac");
        commend.add("2");
        commend.add("-ar");
        commend.add("22050");
        commend.add("-b");
        commend.add("230");
        commend.add("-r");
        commend.add("24");
        commend.add("-y");
        commend.add(dic);
        ProcessBuilder builder = new ProcessBuilder(new String[0]);
        builder.command(commend);
        System.out.println(commend.toString());
        builder.start();
    }

    private static void toImg(String src, String dic, int width, int height) throws IOException {
        ProcessBuilder builder = new ProcessBuilder(new String[0]);
        List commend = new ArrayList();
        commend.add(CMD + "ffmpeg");
        commend.add("-i");
        commend.add(src);
        commend.add("-y");
        commend.add("-f");
        commend.add("image2");
        commend.add("0.001");
        if ((width > 0) && (height > 0)) {
            commend.add("-s");
            commend.add(width + "x" + height);
        }
        commend.add(dic);
        builder.command(commend);
        System.out.println(commend);
        builder.start();
    }

    public static void process(String src, String dic) throws IOException {
        File file = new File(src);
        if ((file == null) || (!file.isFile()) || (!file.exists())) {
            throw new RuntimeException("erro file");
        }
        String name = file.getName();
        int type = getType(name);
        if (type == 1)
            toFlv(src, dic);
        else if (type == 2)
            toAvi(src, dic);
    }

    public static void main(String[] args) {
        try {
            process("d:/mpeg/1.mp4", "d:/mpeg/2/2.avi");
            toImg("d:/mpeg/1.mp4", "d:/mpeg/2/2.jpg", 640, 480);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
